<?php
/**
 * folder base class
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage fs
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
class TestWakFolder extends UnitTestCase {

    function setUp() {
        $this->path=dirname(__FILE__).'/';
        $this->_deleteIfExists($this->path.'new');
        $this->_deleteIfExists($this->path.'renamed');
        $this->_createIfNotExists($this->path.'folder');
        $this->_createIfNotExists($this->path.'old');
        $this->_deleteIfExists($this->path.'folder/old');
    }

    function testCycle() {
        $new=$this->path.'new';
        $f=new WakFolder($new);
        //$this->dump($f);
        $this->assertFalse($f->exists()
            ,"exists() should return false for non-existing folders");
        $this->assertFalse($f->isReadable()
            ,"isReadable() should return false for non-existing folders");
        $this->assertFalse($f->isWritable()
            ,"isWritable() should return false for non-existing folders");
        $this->assertTrue($f->create()
            ,"create() should return true when creating new folder");
        $this->assertTrue(file_exists($new)
            ,"and really create the folder");
        $this->assertTrue($f->exists()
            ,"therefore, exists() should now return true");
        $this->assertTrue($f->isReadable()
            ,"isReadable() should now return true");
        $this->assertTrue($f->isWritable()
            ,"isWritable should now return true");
        $this->assertEqual($f->basename(),'new'
            ,"basename() should return base name");
        $this->assertEqual($f->dirname(),dirname(__FILE__)
            ,"dirname() should return container folder path %s");
        $this->assertEqual($f->realpath(),realpath($new)
            ,"realpath() should return absolute path %s");
        $this->assertTrue($f->delete() and !file_exists($new)
            ,"delete() shall delete folder and return true");
    }

    function testRename() {
        $old=$this->path.'old';
        $renamed=$this->path.'renamed';
        $f=new WakFolder($old);
        //$this->dump($f);
        $this->assertTrue($f->rename('renamed')
            ,"rename() should return true");
        $this->assertTrue(file_exists($renamed) and !file_exists($old)
            ,"...and really rename folder");
    }

    function testMove() {
        $old=$this->path.'old';
        $movedir=$this->path.'folder';
        $moved=$movedir.'/old';
        $f=new WakFolder($old);
        $this->assertTrue($f->move($movedir)
            ,"move($movedir) should return true");
        $this->assertTrue(file_exists($moved) and !file_exists($old)
            ,"...and really move folder");
    }

    /**
     * testing iterator good implementation
     */
    function testIterator() {
        $f=new WakFolder($this->path.'folder');
        $this->dump(get_declared_interfaces());
    }

    function _deleteIfExists($path) {
        if (file_exists($path) and !rmdir($path)) {
            $this->fail("cant' start tests, folder '.$path.' should not exist before tests %s'");
            return false;
        }
        return true;
    }

    function _createIfNotExists($path) {
        if (!file_exists($path) and !mkdir($path,0777)) {
            $this->fail("can't start tests, folder '.$path.' should exist before tests %s'");
            return false;
        }
        $this->pass("created $path");
        return true;
    }
}
